﻿var openid = '';
var bind = 'bind';
$(document).ready(function () {
    $("#loginBox").slideDown("slow");
    onResized();
    openid = HttpUtil.getQueryString('openid');
    bind = HttpUtil.getQueryString("bind");
    if (openid && openid != '' && typeof openid != 'undefined') {
        $("#but_login").removeAttr("disabled");

    }
    else {
        $("#msgLi>p").text("请通过微信公众号绑定页面进入本页面!");
        $("#msgLi").css("display", "block");
        $("#but_login").attr("disabled", "disabled");
    }
    $("#but_login").val(bind == 'bind' ? '绑定' : '解绑');
});

$(window).resize(onResized);

function onResized() {
}

// 点击登录
$('#but_login').click(function (e) {
    submit();
});
//回车登录
$(document).keydown(function (e) {
    if (e.keyCode == 13) {
        submit();
    }
});
//表单提交
function submit() {
    var submit = true;
    //所有带有nullmsg属性的input元素都检查一遍
    $("input[nullmsg]").each(function () {
        if ($("#" + this.name).val() == "") {
            submit = false;
            $("#msgLi>p").text("账号或密码不能为空");
            $("#msgLi").css("display", "block");
            $(this).focus();
            return false;
        }
    });
    if (submit) {
        $("#msgLi").css("display", "none");
        Login();
    }
}

//登录处理函数
function Login() {
    var actionurl = $('form').attr('action');//提交路径
    var checkurl = $('form').attr('check');//验证路径
    var formData = new Object();
    var data = $(":input").each(function () {
        formData[this.name] = $("#" + this.name).val();
    });
    var param = {
        userName: $("#userno").val(),
        password: $("#password").val(),
        openId: openid
    };
    var url = 'User/bindWeixin';
    if (bind == 'unbind') {
        url = 'User/unbindWeixin';
    }
    HttpUtil.get(url, param).success((result)=>{

            //	保存在
            if (result.state == 0) {

                $("#loginBox").hide();
                // $("#msgBox").css("display", "block");
                alert(bind=='bind'?"您已经成功绑定微信！":"您已经成功解绑微信！");
                // $("#msg").text(bind=='bind'?"您已经成功绑定微信！":"您已经成功解绑微信！");
                $("#footer").css("display", "block");

            } else {
                $("#msgLi>p").text(result.errMsg);
                $("#msgLi").css("display", "block");
                return false;
            }

    });
}



// 说明：用 JavaScript 实现网页图片等比例缩放
// 整理：http://www.CodeBit.cn
function DrawImage(ImgD, FitWidth, FitHeight) {
    var image = new Image();
    image.src = ImgD.src;
    if (image.width > 0 && image.height > 0) {
        var tw = FitWidth / image.width;
        var th = FitHeight / image.height;
        var t = tw > th ? tw : th;
        ImgD.width = image.width * tw;
        ImgD.height = image.height * th;
    }
}
